﻿using MySql.Data.MySqlClient;
using System;
using System.Windows.Forms;
using XuanFe.AppLaunch.Common;

namespace XuanFe.AppLaunch
{
    public partial class ConnectionForm : DevExpress.XtraEditors.XtraForm
    {
        public ConnectionForm(string server)
        {
            InitializeComponent();

            this.Server = server;
        }
        public string Server { get; private set; }

        private void ConnectionForm_Load(object sender, EventArgs e)
        {
            try
            {
                this.txtServer.Text = Server;
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                this.Server = this.txtServer.Text;
                MySQLHelper.Instance.Server = this.Server;
                if (!MySQLHelper.Instance.TestConnect())
                {
                    Global.Instance.ErrorDlg("连接数据库失败");
                    return;
                }

                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                this.DialogResult = DialogResult.Cancel;
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }
    }
}
